package org.example.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.example.admin.bo.UserBO;
import org.example.admin.dao.UserDao;
import org.example.admin.dto.UserDto;
import org.example.admin.entity.UserEntity;
import org.example.admin.service.UserRoleService;
import org.example.admin.vo.UserVo;
import org.example.core.utils.PageUtils;
import org.example.core.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.example.admin.service.UserService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

@Service("userService")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;
    @Autowired
    private UserRoleService userRoleService;

    private final static String DEFAULT_PASSWORD = "123456";

    @Override
    public UserEntity queryUserInfo() {
        return null;
    }

    @Override
    public PageUtils list(UserDto userDto) {
        if (userDto.needLimit()){
            PageHelper.startPage(userDto.getPage(), userDto.getPageSize());
        }
        List<UserVo> entityList = userDao.list(userDto);
        return new PageUtils(new PageInfo<>(entityList));
    }

    @Override
    public UserVo detail(Long id) {
        UserVo userVo = userDao.detail(id);
        // 获取到角色信息
        List<Long> roleIds = userRoleService.getURoleIdsByUserId(id);
        userVo.setRoleIds(roleIds);
        return userVo;
    }

    @Override
    @Transactional
    public void save(UserBO user) {
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        if (user.getPassword() == null){
            String encode = bCryptPasswordEncoder.encode(DEFAULT_PASSWORD);
            user.setPassword(encode);
        }else{
            String encode = bCryptPasswordEncoder.encode(user.getPassword());
            user.setPassword(encode);
        }
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        // 保存基本信息
        userDao.save(user);
        // 保存角色
        if (!user.getRoleIds().isEmpty()){
            userRoleService.saveUserRoles(user.getId(), user.getRoleIds());
        }
    }

    @Override
    @Transactional
    public void update(UserBO user) {
        user.setUpdateTime(LocalDateTime.now());
        userDao.update(user);
        // 删除角色
        userRoleService.deleteByUserId(Collections.singletonList(user.getId()));
        if (!user.getRoleIds().isEmpty()){
            // 重新保存
            userRoleService.saveUserRoles(user.getId(), user.getRoleIds());
        }
    }

    @Override
    @Transactional
    public R<?> delete(List<Long> ids) {
        // 删除用户角色
        userRoleService.deleteByUserId(ids);
        int delete = userDao.delete(ids);
        if (delete > 0){
            return R.ok();
        }
        return R.error();
    }
}